package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JPanel;

import pojo.Hinhthucthanhtoan;
import pojo.Hoadon;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.ConvertHelper;
import dao.HinhthucthanhtoanDAO;
import dao.HoadonDAO;
import dao.SanphamDAO;

/**
 * Servlet implementation class TrangGioHang
 */
@WebServlet("/Pages/TrangGioHang.do")
public class TrangGioHang extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TrangGioHang() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		String url = "";
		
		if (action.equals("VaoGioHang")) {

			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}
		
		if (action.equals("TiepTucMua")) {
			String maSP = request.getParameter("maSP");

			
			//---------------------
			//---------------------

			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}
		/*
		if (action.equals("DatHang")) {

			ArrayList<Sanpham> ds = (ArrayList<Sanpham>) session
					.getAttribute("gioHang");
			if (null != ds && ds.size() > 0) {
				request.setAttribute("page", "TrangHoaDon");

				//String tenNguoiNhan = ConvertHelper.GetRequestUnicodeString(
				//		request, "tenNguoiNhan");
				String diaChiNguoiNhan = ConvertHelper.GetRequestUnicodeString(
						request, "diaChiNguoiNhan");
				//String soDienThoai = ConvertHelper.GetRequestUnicodeString(
				//		request, "soDienThoai");

				//request.setAttribute("tenNguoiNhan", tenNguoiNhan);
				request.setAttribute("diaChiNguoiNhan", diaChiNguoiNhan);
				//request.setAttribute("soDienThoai", soDienThoai);
			} else {

				request.setAttribute("page", "TrangGioHang");
			}
			url = "../index.jsp";
		}
		*/
		if (action.equals("CapNhat")) {

			String[] sl = request.getParameterValues("soLuong");
			if (null != sl) {
				ArrayList<Sanpham> ds = (ArrayList<Sanpham>) session
						.getAttribute("gioHang");

				for (int i = 0; i < ds.size(); i++) {
					try {
						ds.get(i)
								.setSoluongtronggiohang(Integer.valueOf(sl[i]));
					} catch (NumberFormatException ex) {
						// Nếu không chuyển về số được thì ta giữ nguyên
					}
				}
				session.setAttribute("gioHang", ds);
			}

			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}
		if (action.equals("ThemVaoGio")) {

			String maSP = request.getParameter("maSP");

			ArrayList<Sanpham> ds = null;
			if (session.getAttribute("gioHang") == null) {

				ds = new ArrayList<Sanpham>();
			} else {
				ds = (ArrayList<Sanpham>) session.getAttribute("gioHang");
			}

			int i = 0;
			for (i = 0; i < ds.size(); i++) {
				if (maSP.equals(String.valueOf(ds.get(i).getMasanpham()))) {
					ds.get(i).setSoluongtronggiohang(
							ds.get(i).getSoluongtronggiohang() + 1);
					break;
				}
			}
			if (ds.size() == i) {
				// Sanpham sp = SanphamDAO.layThongTinSanPham(maSP);
				SanphamDAO dao = new SanphamDAO();
				Sanpham sp = dao.get(Integer.valueOf(maSP));
				sp.setSoluongtronggiohang(1);
				ds.add(sp);
			}

			session.setAttribute("gioHang", ds);

			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}
		if (action.equals("Xoa")) {

			String maSP = request.getParameter("maSP");
			ArrayList<Sanpham> ds = (ArrayList<Sanpham>) session
					.getAttribute("gioHang");
			for (int i = 0; i < ds.size(); i++) {
				String ma = String.valueOf(ds.get(i).getMasanpham());
				if (maSP.equals(ma)) {
					ds.remove(i);
					break;
				}
			}

			session.setAttribute("gioHang", ds);
			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}

		RequestDispatcher rd = request.getRequestDispatcher(url);

		rd.forward(request, response);
	}

}
